package com.tengju.support.infrastructure.persistence.redis;

import com.tengju.support.infrastructure.persistence.redis.dataobject.RedisKeyObject;
import com.tengju.support.infrastructure.persistence.redis.dataobject.RedisKeyPrefix;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.concurrent.TimeUnit;

@Repository
public class WeiXinTokenCacheRepository {

    @Autowired
    private RedisService redisService;

    /**
     * 获取token
     */
    public String getToken() {
        RedisKeyObject redisKeyObject = new RedisKeyObject(RedisKeyPrefix.WEIXIN_TOKEN, "");
        return redisService.getString(redisKeyObject);
    }

    /**
     * 获取chatToken
     */
    public String getChatToken() {
        RedisKeyObject redisKeyObject = new RedisKeyObject(RedisKeyPrefix.WEIXIN_CHAT_TOKEN, "");
        return redisService.getString(redisKeyObject);
    }

    /**
     * 缓存token
     */
    public void cacheToken(String token) {
        if (StringUtils.isEmpty(token)) {
            return;
        }
        RedisKeyObject redisKeyObject = new RedisKeyObject(RedisKeyPrefix.WEIXIN_TOKEN, "");
        redisService.setValue(redisKeyObject, token, 90, TimeUnit.MINUTES);
    }

    /**
     * 缓存chatToken
     */
    public void cacheChatToken(String token) {
        if (StringUtils.isEmpty(token)) {
            return;
        }
        RedisKeyObject redisKeyObject = new RedisKeyObject(RedisKeyPrefix.WEIXIN_CHAT_TOKEN, "");
        redisService.setValue(redisKeyObject, token, 90, TimeUnit.MINUTES);
    }

    /**
     * 删除token
     */
    public void deleteToken() {
        RedisKeyObject redisKeyObject = new RedisKeyObject(RedisKeyPrefix.WEIXIN_TOKEN, "");
        redisService.delKey(redisKeyObject);
    }

    /**
     * 删除chatToken
     */
    public void deleteChatToken() {
        RedisKeyObject redisKeyObject = new RedisKeyObject(RedisKeyPrefix.WEIXIN_CHAT_TOKEN, "");
        redisService.delKey(redisKeyObject);
    }

    /**
     * 获取登录token
     */
    public String getLoginToken() {
        RedisKeyObject redisKeyObject = new RedisKeyObject(RedisKeyPrefix.WECHAT_LOGIN, "");
        return redisService.getString(redisKeyObject);
    }

    /**
     * 缓存登录token
     */
    public void cacheLoginToken(String token) {
        if (StringUtils.isEmpty(token)) {
            return;
        }
        RedisKeyObject redisKeyObject = new RedisKeyObject(RedisKeyPrefix.WECHAT_LOGIN, "");
        redisService.setValue(redisKeyObject, token, 90, TimeUnit.MINUTES);
    }

}
